var express = require('express');
var router = express.Router();
var order = require("../models/order");
var getOpenid = require('../wechat/wechat.js');


//获取商品数据
router.get('/', async function(req, res, next) {

    let string = await getOpenid(req.param("code"));
    let str = JSON.parse(string);
    let openid = str.openid;

    let page = parseInt(req.param("searchNum"));
    let pageSize = 5;
    // let sort = req.param("sort");
    let skip = (page-1)*pageSize;
    let params = {
        ifCancel: false,
        openid:openid
    };

    if (req.param("ifPay") != undefined){
        params.ifPay=req.param("ifPay")
    }
    if (req.param("ifGet") != undefined){
        params.ifGet=req.param("ifGet")
    }
    console.log(params)
    let model = order.find(params).skip(skip).limit(pageSize).sort('-date');

    model.exec(function (err1, result) {
        if (err1) {
            res.json({
                status: "1",
                msg: err.message
            })
        } else {
            res.json({
                status: "0",
                msg: "",
                result: {
                    list: result
                }
            })
        }
    });

});


module.exports = router;